﻿using System;
using System.Linq;
using System.Collections.Generic;
using NUnit.Framework;
using AutoMapper;

namespace NMA.Infrastructure.NHibernate.Tests.Repository
{
    using NMA.Domain.Model;
    using NMA.Infrastructure.NHibernate.DomainObject;
    //using NMA.Infrastructure.NHibernate.AutoMapper;        

    // http://jasondentler.com/blog/category/fluent-nhibernate/
    // http://daniel.wertheim.se/tag/fluent-nhibernate/
    // http://www.bengtbe.com/blog/post/2009/10/08/NerdDinner-with-Fluent-NHibernate-Part-3-The-infrastructure.aspx
    // http://dotnetslackers.com/articles/ado_net/Your-very-first-NHibernate-application-Part-1.aspx
    // http://nhforge.org/wikis/howtonh/currentsessioncontext-for-desktop-development.aspx
    // http://hackingon.net/?tag=/design
    // http://stackoverflow.com/questions/2303540/taking-advantage-of-hibernate-lazy-loading
    // http://www.codeproject.com/KB/custom-controls/Fluent-MembershipProvider.aspx
    [TestFixture]
    public class CategoryRepositoryTest : TestingBase
    {
        [Test]
        public void Can_count_all_category()
        {
            int result = -1;

            if (CategoryRepository != null)
            {
                result = CategoryRepository.CountAllCategory();

                Assert.IsTrue(result >= 0, "contain " + result + " item in database");
            }
        }

        [Test]
        public void Can_get_by_id()
        {
            ICategory entity = null;

            if (CategoryRepository != null)
            {
                var obj = CategoryList.FirstOrDefault<Category>();

                entity = CategoryRepository.GetById(obj.Id);

                Assert.IsNotNull(entity, "Category is not null");
                Assert.IsTrue(entity.Id == obj.Id);
                Assert.IsTrue(entity.Name == obj.Name);
                Assert.IsTrue(entity.CreatedDate.ToShortDateString() == obj.CreatedDate.ToShortDateString());
            }
        }        

        [Test]
        public void Update_category_to_database()
        {
            if (CategoryRepository != null)
            {
                var entity = CategoryList.FirstOrDefault<Category>();

                var dbObj = CategoryRepository.GetById(entity.Id);

                Assert.IsNotNull(dbObj);

                dbObj.Name = "update name";
                dbObj.UpdatedDate = DateTime.Now.AddDays(1);

                // update into database
                CategoryRepository.Update(dbObj);

                // get out for assertion
                var updateObj = CategoryRepository.GetById(dbObj.Id);

                Assert.IsNotNull(updateObj);
                Assert.IsTrue(updateObj.Id == dbObj.Id);
                Assert.IsTrue(updateObj.Name == "update name");
                Assert.IsTrue(updateObj.UpdatedDate.ToShortDateString() == DateTime.Now.AddDays(1).ToShortDateString());
            }
        }

        [Test]
        public void Find_by_category_from_database()
        {
            IEnumerable<ICategory> result = null;

            if (CategoryRepository != null)
            {
                result = CategoryRepository.GetBy(x => x.Name == "unit test name 1");

                Assert.IsNotNull(result);

                var entity = result.FirstOrDefault<ICategory>();

                Assert.IsNotNull(entity);
                Assert.IsTrue(entity.Name == "unit test name 1");
                Assert.IsTrue(entity.CreatedDate.ToShortDateString() == DateTime.Now.ToShortDateString());
            }
        }

        [Test]
        public void Get_all_category_from_database_with_paging()
        {
            if (CategoryRepository != null)
            {
                var result = CategoryRepository.All(1, 10);

                Assert.IsTrue(result.TotalCount >= 2, "Total count");
                Assert.IsTrue(result.PageIndex == 1, "Page index");
                Assert.IsTrue(result.PageSize == 10, "Page size");
            }
        }

        //[Test]
        //public void Test()
        //{
        //    NMA.Infrastructure.NHibernate.Service.Handler.CategoryAuditHandler handler = new NMA.Infrastructure.NHibernate.Service.Handler.CategoryAuditHandler();
        //    NMA.Infrastructure.NHibernate.Service.Event.CategoryAuditEvent catEvent = new NMA.Infrastructure.NHibernate.Service.Event.CategoryAuditEvent()
        //    {
        //        UserName = "abc",
        //        UserAction = "test action",
        //        DateEvent = DateTime.Now.AddDays(1)
        //    };

        //    handler.Handle(catEvent);

        //    // auto mapper test            
        //    var destination1 = Mapper.Map<NMA.Infrastructure.NHibernate.Service.Event.CategoryAuditEvent, Audit>((new NMA.Infrastructure.NHibernate.Service.Event.CategoryAuditEvent()
        //    {                
        //        UserName = "abc",
        //        UserAction = "test action",
        //        DateEvent = DateTime.Now.AddDays(1)
        //    }));
        //}
    }
}